package com.cqgc.aop;
import com.alibaba.fastjson.JSON;
import com.cqgc.mapper.LogsMapper;
import com.cqgc.pojo.entity.Logs;
import com.cqgc.pojo.entity.User;
import com.cqgc.utils.HttpContextUtils;
import com.cqgc.utils.IpUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
@Component
@Aspect
@Slf4j
public class LogAspect {
    @Autowired
    private LogsMapper logMapper;
    @Pointcut("@annotation(com.cqgc.aop.LogAnnotation)")
    public void put() {
    }
    //环绕通知
    @Around("put()")
    public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
        long beginTime = System.currentTimeMillis();
        //执行方法
        Object result = joinPoint.proceed();
        //执行时间(毫秒)
        long endTime = System.currentTimeMillis() - beginTime;
        //保存日志
        recordLog(joinPoint,endTime);
        return result;
    }
    public void recordLog(ProceedingJoinPoint joinPoint, long time) {
        MethodSignature signature =(MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        LogAnnotation logAnnotation = method.getAnnotation(LogAnnotation.class);
        log.info("=======================log.start=========================");
        log.info("module:{}",logAnnotation.module());
        log.info("operation:{}",logAnnotation.operator());
        //请求的方法名
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = signature.getName();
        log.info("request method:{}",className + "." + methodName + "()");
        //请求参数
        Object[] args = joinPoint.getArgs();
        String params = JSON.toJSONString(args[0]);
        log.info("params:{}",params);
        //获取request 设置IP地址
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        String ipAddr = IpUtils.getIpAddr(request);
        log.info("ip:{}", ipAddr);
        log.info("execute time:{} ms",time);
        log.info("=======================log.end=========================");
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()){
            User principal = (User) subject.getPrincipal(); // 获取主体标识，如用户名
            System.out.println(principal);
            String account = principal.getAccount();
            System.out.println(subject);
            Logs logEntity = new Logs();
            logEntity.setAccount(account);
            logEntity.setModule(logAnnotation.module());
            logEntity.setOperation(logAnnotation.operator());
            logEntity.setClassName(className);
            logEntity.setMethodName(methodName);
            logEntity.setRequestParams(params);
            logEntity.setClientIp(ipAddr);
            logEntity.setExecuteTime(time);
            int i = logMapper.addLog(logEntity);
            if (i>0){
                log.info("日志记录成功");
            }else
            {
                log.error("日志记录失败");
            }
        }

    }
}
